﻿"use strict";
define(['admin/app', "service/admin/tokenShopSubjectService", "angular-kindeditor", "ui.file.manage"], function (app) {
    app.controller('tokenShopSubjectController', function ($scope, $stateParams, $location, $uibModal, $state, utils, tokenShopSubjectService) {
        $scope.query = {
            page: 1, size: 20, q: '', title:''
        };
        $scope.product_query = {
            page: 1, size: 20, subject_id: 0, keyword:''
        };
        $scope.subject_product_query = {
            page: 1, size: 10, subject_id: 0, product_name: ''
        }
        $.extend($scope.query, $stateParams);
        //商品弹窗选择
        var data = "";
        var index = "";
        $scope.addGood = function () {
            index = $stateParams.id;
            var modalInstance = $uibModal.open({
                templateUrl: 'modal.html',//script标签中定义的id
                controller: 'modalCtrl',//modal对应的Controller
                windowClass: 'large-Modal',
                resolve: {
                    data: function () {//data作为modal的controller传入的参数
                        //获取商品列表
                        $scope.product_query.subject_id = $stateParams.id;
                        return data = tokenShopSubjectService.getproducts($scope.product_query).success(function (response) {
                            return response.data;//用于传递数据
                        });
                    },
                    index: function () {
                        //对应分组索引
                        return index;
                    },
                    parentScope: function () {
                        return $scope;
                    }
                }
            })
        };

        $scope.auditSort = function (id) {
            index = id;
            var modalInstance = $uibModal.open({
                templateUrl: 'modal1.html',//script标签中定义的id
                controller: 'modalCtrl1',//modal对应的Controller
                windowClass: 'pop',
                resolve: {
                    index: function () {
                        //对应分组索引
                        return index;
                    },
                    parentScope: function () {
                        return $scope;
                    }
                }
            })
        };


        //模态框对应的Controller
        app.controller('modalCtrl', function ($scope, $uibModalInstance, parentScope, data, index) {
            $scope.data = data.data.data;
            //在这里处理要进行的操作
            $scope.ok = function () {
                var ids = $("input[type='checkbox']:checked");
                if (ids.length == 0) {
                    utils.notify("暂无商品", "warning");
                    $uibModalInstance.close();
                    return;
                }
                let product_dtos = [];
                angular.forEach(ids, function (item, key) {
                    let product_dto = {
                        subject_id: $stateParams.id,
                        product_id: item.value
                    }
                    product_dtos.push(product_dto);
                });
                tokenShopSubjectService.addproducts(product_dtos).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    parentScope.getSubjectProducts();
                });
                $uibModalInstance.close();
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };

            $scope.search_page = function (page) {
                parentScope.product_query.page = page;
                tokenShopSubjectService.getproducts(parentScope.product_query).success(function (response) {
                    $scope.data = response.data;//用于传递数据
                });
            };

            $scope.getSubjectProducts = function () {
                parentScope.product_query.keyword = $scope.keyword;
                tokenShopSubjectService.getproducts(parentScope.product_query).success(function (response) {
                    $scope.data = response.data;//用于传递数据
                });
            };
        });

        //模态框对应的Controller
        app.controller('modalCtrl1', function ($scope, $uibModalInstance, index, parentScope) {
            //在这里处理要进行的操作
            $scope.ok = function () {
                let product_dto = {
                    id: index,
                    sort: $scope.model.sort
                };

                tokenShopSubjectService.altersort(product_dto).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    parentScope.getSubjectProducts();
                });
                $uibModalInstance.close();
            };

            $scope.cancel = function () {
                $uibModalInstance.dismiss('cancel');
            };
        });
        var methods = {
            search: function () {
                tokenShopSubjectService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    tokenShopSubjectService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    tokenShopSubjectService.get({ id: 0 }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '新增';
                }
            },
            save: function () {
                var modal = utils.confirm({ msg: "确定要保存该专题么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    model.topic_link = model.topic_link == null ? "" : model.topic_link;
                    $scope.psdisabled = true;
                    if (model.id == 0) {
                        tokenShopSubjectService.add(model).success(function (response) {
                            utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                            if (response.result_code == 1000)
                                methods.goBack();
                            else
                                $scope.psdisabled = false;
                        }).error(function () { $scope.psdisabled = false; });
                    } else {
                        tokenShopSubjectService.update(model).success(function (response) {
                            utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                            if (response.result_code == 1000)
                                methods.goBack();
                            else
                                $scope.psdisabled = false;
                        }).error(function () { $scope.psdisabled = false; });
                    }
                });
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除该专题", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenShopSubjectService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            updateStatus: function (id, status) {
                var tip = "停用";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该专题", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenShopSubjectService.updateStatus(id, status).success(function () {
                        methods.search();
                    });
                })
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
            getSubjectProducts: function () {
                $scope.subject_product_query.subject_id = $stateParams.id;
                tokenShopSubjectService.getsubjectproducts($scope.subject_product_query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            deleteProduct: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除该商品", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenShopSubjectService.deleteproduct(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        methods.getSubjectProducts();
                    });
                })
            },
            alterSort: function (id) {
                tokenShopSubjectService.alterSort(id).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    methods.getSubjectProducts();
                });
            },
            product_page: function (page) {
                $scope.subject_product_query.page = page;
                methods.getSubjectProducts('page', $scope.query.page);
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.tokenShopSubject.detail')) {
            methods.edit();
        }
        else if ($state.includes('app.tokenShopSubject.product_list')) {
            methods.getSubjectProducts();
        }
        else {
        	methods.search();
    	}
    });
});
